﻿using Castle.DynamicProxy;
using Kernel.Core.General.Diagnostic;
using Microsoft.Extensions.DiagnosticAdapter;
using System.Diagnostics;

namespace WebAPI.Configure;

public class KernelDiagnosticListener
{
    public static void Subscribe()
    {
        var listener = ServiceHost.Current.GetService<DiagnosticListener>();
        listener.SubscribeWithAdapter(new KernelDiagnosticListener());
    }

    //发布的消息主题名称
    [DiagnosticName(DiagnosticConst.EVENT_KERNEL_METHOD_START)]
    //发布的消息参数名称和发布的属性名称要一致
    public void MethodStart(IInvocation invocation)
    {
        Debug.WriteLine("> {0}.{1}，参数：{2}... ",
           invocation.MethodInvocationTarget.ReflectedType.FullName,
           invocation.Method.Name,
           string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));

    }

    //发布的消息主题名称
    [DiagnosticName(DiagnosticConst.EVENT_KERNEL_METHOD_END)]
    //发布的消息参数名称和发布的属性名称要一致
    public void MethodEnd(IInvocation invocation)
    {
        Debug.WriteLine("< {0}.{1}，返回结果：{2}",
           invocation.MethodInvocationTarget.ReflectedType.FullName,
           invocation.Method.Name,
           invocation.ReturnValue);

    }
}
